home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / tdddconv.lha / writemif.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-01  |  6.7 KB  |  220 lines

  1. /* writemif.c - routines to output FrameMaker MIF format
  2.  *            - using TTDDDLIB by Glenn M. Lewis - 7/24/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writemif.c,v 1.5 1991/11/01 16:18:54 glewis Exp glewis $";
  6.  
  7. #include <stdio.h>
  8. #include "ttdddlib.h"
  9.  
  10. /* double XSIZE = 8.5;    - in the PostScript module */
  11. /* double YSIZE = 11.0;    - in the PostScript module */
  12. #define MARGIN    (0.25)    /* On each side of the page */
  13. #define PPI    (72.0)
  14. #define COS60    (0.5)
  15. #define COS30    (0.8660)
  16.  
  17. void write_MIF_header();
  18. void draw_MIF_view();
  19. void render_MIF_object();
  20.  
  21. static FILE *out;
  22. static WORLD *world;
  23. static MBB mbb;
  24. static double xsize, ysize;
  25. static int view;
  26. static double xoffset, yoffset, scale, ybottom;
  27.  
  28. static int tab;
  29. static void dotab() { register int i=tab; while (i--) fputc(' ',out); }
  30. #define TAB(a) {dotab();fputs(a,out);}
  31. #define TABr(a) {dotab();fputs(a,out);tab++;}
  32. #define TABl(a) {tab--;dotab();fputs(a,out);}
  33. #define PTAB dotab();fprintf
  34.  
  35. int write_MIF(object, file, selected_view)
  36. WORLD *object;
  37. FILE *file;
  38. int selected_view;
  39. {
  40.     double llx, lly;
  41.     register OBJECT *obj;
  42.     if (!(out = file)) return(0);        /* File not open */
  43.     if (!(world = object)) return(0);    /* No object */
  44.  
  45.     /* Determine the extent of the world object */
  46.     mbb.minx = mbb.miny = mbb.minz =  1.0e10;
  47.     mbb.maxx = mbb.maxy = mbb.maxz = -1.0e10;
  48.     for (obj=world->object; obj; obj=obj->next) {
  49.         calculate_MBB(obj);
  50.         if (!obj->user) continue;
  51.         if (((MBB*)obj->user)->minx<mbb.minx) mbb.minx = ((MBB*)obj->user)->minx;
  52.         if (((MBB*)obj->user)->miny<mbb.miny) mbb.miny = ((MBB*)obj->user)->miny;
  53.         if (((MBB*)obj->user)->minz<mbb.minz) mbb.minz = ((MBB*)obj->user)->minz;
  54.         if (((MBB*)obj->user)->maxx>mbb.maxx) mbb.maxx = ((MBB*)obj->user)->maxx;
  55.         if (((MBB*)obj->user)->maxy>mbb.maxy) mbb.maxy = ((MBB*)obj->user)->maxy;
  56.         if (((MBB*)obj->user)->maxz>mbb.maxz) mbb.maxz = ((MBB*)obj->user)->maxz;
  57.     }
  58. #ifdef DEBUG
  59.     fprintf(stderr, "MBB: (%g,%g,%g)-(%g,%g,%g)\n",
  60.         mbb.minx, mbb.miny, mbb.minz,
  61.         mbb.maxx, mbb.maxy, mbb.maxz);
  62. #endif
  63.  
  64.     write_MIF_header();
  65.     if (selected_view==VIEW_ALL_FOUR) {
  66.         for (view=0; view<4; view++) {
  67.             llx = ((view&0x02) ? XSIZE/2.0 : MARGIN) * PPI;
  68.             lly = ((view&0x01) ? YSIZE/2.0 : MARGIN) * PPI;
  69.             xsize = PPI*(XSIZE/2.0 - MARGIN);
  70.             ysize = PPI*(YSIZE/2.0 - MARGIN);
  71.             TABr("<Frame\n");
  72.             TAB("<Pen 0>\n");
  73.             TAB("<Fill 15>\n");
  74.             TAB("<PenWidth  0.1 pt>\n");
  75.             TAB("<Separation 0>\n");
  76.             PTAB(out, "<BRect %g %g %g %g>\n", llx, lly, xsize, ysize);
  77.             draw_MIF_view();
  78.             TABl("> # end of Frame\n");
  79.         }
  80.     } else {
  81.         xsize = PPI*(XSIZE - 2.0*MARGIN);
  82.         ysize = PPI*(YSIZE - 2.0*MARGIN);
  83.         view=selected_view;
  84.         TABr("<Frame\n");
  85.         TAB("<Pen 0>\n");
  86.         TAB("<Fill 15>\n");
  87.         TAB("<PenWidth  0.1 pt>\n");
  88.         TAB("<Separation 0>\n");
  89.         PTAB(out, "<BRect %g %g %g %g>\n", PPI*MARGIN, PPI*MARGIN, xsize, ysize);
  90.         draw_MIF_view();
  91.         TABl("> # end of Frame\n");
  92.     }
  93.     return(1);
  94. }
  95.  
  96. static void write_MIF_header()
  97. {
  98.     tab=0;
  99.     fputs("<MIFFile 2.00>\t# Created by TTDDDLIB by Glenn M. Lewis\n", out);
  100.     fprintf(out, "# %s\n", rcs_id);
  101.     fputs("\n<Document\n <DPageRounding DeleteEmptyPages>\n", out);
  102.     fputs(" <DSmartQuotesOn Yes>\n <DBordersOn Yes>\n <DGridOn Yes>\n", out);
  103.     fputs(" <DRulersOn Yes>\n <DSymbolsOn No>\n> # end of Document\n", out);
  104.     fputs("<Units Upt>\n", out);
  105. }
  106.  
  107. static void draw_MIF_view()
  108. {
  109.     double xratio, yratio, zratio, ratio;
  110.     OBJECT *obj;
  111.  
  112.     if (mbb.maxx-mbb.minx>0.0) xratio=1.0/(mbb.maxx-mbb.minx); else xratio=1.0;
  113.     if (mbb.maxy-mbb.miny>0.0) yratio=1.0/(mbb.maxy-mbb.miny); else yratio=1.0;
  114.     if (mbb.maxz-mbb.minz>0.0) zratio=1.0/(mbb.maxz-mbb.minz); else zratio=1.0;
  115.     ratio = (xratio < yratio ? xratio : yratio);    /* Get minimum ratio */
  116.     ratio = (ratio  < zratio ? ratio  : zratio);
  117.  
  118.     scale = (xsize < ysize ? xsize : ysize)*ratio;    /* Keep aspect ratio same */
  119.  
  120.     switch(view) {
  121.         case VIEW_TOP:
  122.             xoffset = (xsize-scale/xratio)/2.0;
  123.             yoffset = (ysize-scale/yratio)/2.0;
  124.             break;
  125.         case VIEW_FRONT:
  126.             xoffset = (xsize-scale/xratio)/2.0;
  127.             yoffset = (ysize-scale/zratio)/2.0;
  128.             break;
  129.         case VIEW_ISO:
  130.             xratio = (mbb.maxx-mbb.minx)*COS30+(mbb.maxy-mbb.miny)*COS30;
  131.             if (xratio>0.0) xratio = 1.0/xratio; else xratio=1.0;
  132.             ybottom = -(mbb.maxx-mbb.minx)*COS60;
  133.             yratio = (mbb.maxz-mbb.minz)+(mbb.maxy-mbb.miny)*COS60    /* Top */
  134.                      - ybottom;
  135.             if (yratio>0.0) yratio = 1.0/yratio; else yratio=1.0;
  136.             ratio = (xratio < yratio ? xratio : yratio);
  137.             scale = (xsize < ysize ? xsize : ysize)*ratio;
  138.             xoffset = (xsize-scale/xratio)/2.0;
  139.             yoffset = (ysize-scale/yratio)/2.0;
  140.             break;
  141.         case VIEW_RIGHT:
  142.             xoffset = (xsize-scale/yratio)/2.0;
  143.             yoffset = (ysize-scale/zratio)/2.0;
  144.             break;
  145.     }
  146.     for (obj=world->object; obj; obj=obj->next)
  147.         render_MIF_object(obj);
  148. }
  149.  
  150. static void render_MIF_object(obj)
  151. register OBJECT *obj;
  152. {
  153.     register UWORD *f;
  154.     register OBJECT *op;
  155.     register int i, j;
  156.     int p[3];
  157.     double x[3], y[3];
  158.  
  159.     if (!obj->desc) return;
  160.     for (op=obj->child; op; op=op->next)    /* render children first */
  161.         render_MIF_object(op);
  162.     
  163.     /* Now, the meat... */
  164.     if (obj->desc->shap && obj->desc->shap[1]!=0) return;    /* A lamp */
  165.     if (!obj->desc->edge || !obj->desc->pnts || !obj->desc->face) return;
  166.     for (f=obj->desc->face,i=obj->desc->fcount; i--; f+=3) {
  167.         p[0] = obj->desc->edge[((*f)<<1)];
  168.         p[1] = obj->desc->edge[((*f)<<1)+1];
  169.         if (obj->desc->edge[((f[1])<<1)] == p[0] ||
  170.             obj->desc->edge[((f[1])<<1)] == p[1])
  171.             p[2] = obj->desc->edge[((f[1])<<1)+1];
  172.         else
  173.             p[2] = obj->desc->edge[((f[1])<<1)];
  174.         switch(view) {
  175.             case VIEW_TOP:
  176.                 for (j=3; j--; ) {
  177.                     x[j] = obj->desc->pnts[p[j]].val[0]-mbb.minx;
  178.                     y[j] = obj->desc->pnts[p[j]].val[1]-mbb.miny;
  179.                 }
  180.                 break;
  181.             case VIEW_FRONT:
  182.                 for (j=3; j--; ) {
  183.                     x[j] = obj->desc->pnts[p[j]].val[0]-mbb.minx;
  184.                     y[j] = obj->desc->pnts[p[j]].val[2]-mbb.minz;
  185.                 }
  186.                 break;
  187.             case VIEW_ISO:
  188.                 for (j=3; j--; ) {
  189.                     x[j] = (obj->desc->pnts[p[j]].val[0]-mbb.minx)*COS30
  190.                           +(obj->desc->pnts[p[j]].val[1]-mbb.miny)*COS30;
  191.                     y[j] = (obj->desc->pnts[p[j]].val[2]-mbb.minz)
  192.                           -(obj->desc->pnts[p[j]].val[0]-mbb.minx)*COS60
  193.                           +(obj->desc->pnts[p[j]].val[1]-mbb.miny)*COS60
  194.                           - ybottom;
  195.                 }
  196.                 break;
  197.             case VIEW_RIGHT:
  198.                 for (j=3; j--; ) {
  199.                     x[j] = obj->desc->pnts[p[j]].val[1]-mbb.miny;
  200.                     y[j] = obj->desc->pnts[p[j]].val[2]-mbb.minz;
  201.                 }
  202.                 break;
  203.         }
  204.         if (x[0]==x[1] && x[1]==x[2] && y[0]==y[1] && y[1]==y[2]) continue;
  205.         TABr("<Polygon\n");
  206.         PTAB(out, "<Point %0.2lf %0.2lf>\n", xoffset+scale*x[0],
  207.             ysize-(yoffset+scale*y[0]));
  208.         if (!(x[0]==x[1] && y[0]==y[1])) {
  209.             PTAB(out, "<Point %0.2lf %0.2lf>\n", xoffset+scale*x[1],
  210.                 ysize-(yoffset+scale*y[1]));
  211.         }
  212.         if (!(x[0]==x[2] && y[0]==y[2]) && !(x[1]==x[2] && y[1]==y[2])) {
  213.             PTAB(out, "<Point %0.2lf %0.2lf>\n", xoffset+scale*x[2],
  214.                 ysize-(yoffset+scale*y[2]));
  215.         }
  216.         TABl(">\n");
  217.     }
  218. }
  219.  
  220.